﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class AddTC : System.Web.UI.Page
{
    class_database _db;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] == null)
        {
            Session["transfer"] = "login";
            string url = "~/Default.aspx";
            Response.Redirect(url);
        }
        else
        {
            Session["transfer"] = null;
        }
    }

    static string IDNguyenNhan;
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        Button button = (sender as Button);
        IDNguyenNhan = button.CommandArgument;
        string sql = "SELECT rule_nguyennhan.`Name` as NguyenNhan, rule_nguyennhan.CF as CF, khacphuc.KhacPhuc as KhacPhuc FROM rule_nguyennhan, khacphuc WHERE rule_nguyennhan.ID = khacphuc.IDNguyenNhan AND rule_nguyennhan.ID = " + IDNguyenNhan;
        _db = new class_database();
        DataTable dt = _db.GetDataTable(sql);
        txtNguyenNhan.Text = Convert.ToString(dt.Rows[0]["NguyenNhan"]);
        txtCF.Text = Convert.ToString(dt.Rows[0]["CF"]);
        txtKhacPhuc.Text = Convert.ToString(dt.Rows[0]["KhacPhuc"]);
        span1.Attributes.Add("style", "display: none");
        span2.Attributes.Add("style", "display: none");
        PopupEdit.Attributes.Add("style", "display: block");
    }


    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Button button = (sender as Button);
        IDNguyenNhan = button.CommandArgument;
        _db = new class_database();
        string where = "IDNguyenNhan = " + IDNguyenNhan;
        _db.DeleteRecord("khacphuc", where);
        where = "ID = " + IDNguyenNhan;
        _db.DeleteRecord("rule_nguyennhan", where);
        Response.Redirect(Request.RawUrl, true);
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        span1.Attributes.Add("style", "display: none");
        span2.Attributes.Add("style", "display: none");
        if (txtNguyenNhan.Text == "" || txtKhacPhuc.Text == "" || txtCF.Text == "")
        {
            span1.Attributes.Add("style", "display: block");
            return;
        }

        double currentCF = Math.Round(Convert.ToDouble(txtCF.Text), 2);
        double remainCF = 0;
        string IDTrieuChung;
        string sql = "SELECT IDTrieuChung FROM rule_nguyennhan WHERE ID = " + IDNguyenNhan;
        _db = new class_database();
        DataTable dt = _db.GetDataTable(sql);
        IDTrieuChung = dt.Rows[0]["IDTrieuChung"].ToString();
        sql = "SELECT CF FROM rule_nguyennhan WHERE IDTrieuChung = " + IDTrieuChung + " AND ID != " + IDNguyenNhan;
        dt = _db.GetDataTable(sql);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            remainCF += Math.Round(Convert.ToDouble(dt.Rows[i]["CF"]), 2);
        }

        double checkCF = remainCF + currentCF;
        if (checkCF > 1.00)
        {
            span2.Attributes.Add("style", "display: block");
            return;
        }
        _db = new class_database();
        string[] _param = {"Name", "CF"};
        string[] _value = {txtNguyenNhan.Text, txtCF.Text};
        string _where = "rule_nguyennhan.ID = " + IDNguyenNhan;
        _db.UpdateRecord("rule_nguyennhan", _param, _value, _where);
        string[] _param2 = { "KhacPhuc" };
        string[] _value2 = { txtKhacPhuc.Text };
        string _where2 = "khacphuc.ID = " + IDNguyenNhan;
        _db.UpdateRecord("khacphuc", _param2, _value2, _where2);
        Response.Redirect(Request.RawUrl, true);
        PopupEdit.Attributes.Add("style", "display: none");
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataToRadioList();
        rptNguyenNhan.DataSource = null;
        rptNguyenNhan.DataBind();
        divLine.Attributes.Add("style", "display: none");
        divButtonThemNguyenNhan.Attributes.Add("style", "display: none");
    }
    static string IDTrieuChung;
    protected void radioButtonList_SelectedIndexChanged(object sender, EventArgs e)
    {
        string trieuChung = radioButtonList.SelectedValue.ToString();
        string sql = "SELECT trieuchung.ID as IDTrieuChung, rule_nguyennhan.ID as ID, rule_nguyennhan.CF, rule_nguyennhan.`Name` as NguyenNhan, khacphuc.KhacPhuc FROM rule_nguyennhan, trieuchung, khacphuc WHERE rule_nguyennhan.IDTrieuChung = trieuchung.ID AND khacphuc.IDNguyenNhan = rule_nguyennhan.ID AND trieuchung.`Name` = '" + trieuChung + "' GROUP BY CF DESC, ID";
        _db = new class_database();
        DataTable dt = _db.GetDataTable(sql);
        IDTrieuChung = dt.Rows[0]["IDTrieuChung"].ToString();
        rptNguyenNhan.DataSource = dt.DefaultView;
        rptNguyenNhan.DataBind();
        divLine.Attributes.Add("style", "display: block");
        divButtonThemNguyenNhan.Attributes.Add("style", "display: block");  
    }
    private void BindDataToRadioList()
    {
        DataTable dt = new DataTable();
        string species = DropDownList1.SelectedItem.Value;
        string sql = "select Name, Species, ID from trieuchung where Species = '" + species + "'";
        _db = new class_database();
        dt = _db.GetDataTable(sql);
        radioButtonList.DataSource = dt;
        radioButtonList.DataValueField = "Name";
        radioButtonList.DataBind();
    }
    protected void rptNguyenNhan_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        
    }



    protected void btnCancel_Click(object sender, EventArgs e)
    {
        PopupEdit.Attributes.Add("style", "display: none");
    }
    protected void btnThemNguyenNhan_Click(object sender, EventArgs e)
    {
        TextBox4.Text = "";
        TextBox5.Text = "";
        TextBox6.Text = "";
        span5.Attributes.Add("style", "display: none");
        span6.Attributes.Add("style", "display: none");
        string sql = "SELECT rule_nguyennhan.CF FROM rule_nguyennhan, trieuchung WHERE rule_nguyennhan.IDTrieuChung = trieuchung.ID AND trieuchung.ID = " + IDTrieuChung;
        _db = new class_database();
        DataTable dt = _db.GetDataTable(sql);
        double checkSumCF = 0;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            checkSumCF += Math.Round(Convert.ToDouble(dt.Rows[i]["CF"]), 2);
        }
        double difference = 1 * 0.01;
        if (Math.Abs(checkSumCF - 1.00) < difference)
        {
            PopupThongBao.Attributes.Add("style", "display: block");
        }
        else
        {
            PopupThemNguyenNhan.Attributes.Add("style", "display: block");
        }
    }
    protected void btnThemTrieuChung_Click(object sender, EventArgs e)
    {
        txtTrieuChung.Text = "";
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        span3.Attributes.Add("style", "display: none");
        span4.Attributes.Add("style", "display: none");
        PopupThemTrieuChung.Attributes.Add("style", "display: block");
    }
    protected void btnOK_Click2(object sender, EventArgs e)
    {
        span3.Attributes.Add("style", "display: none");
        span4.Attributes.Add("style", "display: none");
        if (txtTrieuChung.Text == "" || TextBox1.Text == "" || TextBox2.Text == "" || TextBox3.Text == "")
        {
            span3.Attributes.Add("style", "display: block");
            return;
        }
        double checkCF = 0;
        checkCF = Math.Round(Convert.ToDouble(TextBox3.Text), 2);
        if (checkCF > 1.0)
        {
            span4.Attributes.Add("style", "display: block");
            return;
        }
        _db = new class_database();
        string[] _param = {"Name", "Species"};
        string[] _value = {txtTrieuChung.Text.ToString(), DropDownList1.Text.ToString()};
        _db.InsertRecord("trieuchung", _param, _value);
        string[] _param2 = { "IDTrieuChung", "Name", "CF" };
        string sql = "SELECT ID FROM trieuchung ORDER BY ID DESC LIMIT 1";
        DataTable dt = _db.GetDataTable(sql);
        string IDTrieuChung = dt.Rows[0]["ID"].ToString();
        string[] _value2 = { IDTrieuChung, TextBox1.Text.ToString(), TextBox3.Text.ToString() };
        _db.InsertRecord("rule_nguyennhan", _param2, _value2);
        string[] _param3 = { "IDNguyenNhan", "KhacPhuc" };
        sql = "SELECT ID FROM rule_nguyennhan ORDER BY ID DESC LIMIT 1";
        dt = _db.GetDataTable(sql);
        string IDNguyenNhan = dt.Rows[0]["ID"].ToString();
        string[] _value3 = {IDNguyenNhan, TextBox2.Text.ToString()};
        _db.InsertRecord("khacphuc", _param3, _value3);
        PopupThemTrieuChung.Attributes.Add("style", "display: none");
    }
    protected void btnCancel_Click2(object sender, EventArgs e)
    {
        PopupThemTrieuChung.Attributes.Add("style", "display: none");
    }
    protected void btnOK_Click3(object sender, EventArgs e)
    {
        PopupThongBao.Attributes.Add("style", "display: none");
    }
    protected void btnCancel_Click4(object sender, EventArgs e)
    {
        PopupThemNguyenNhan.Attributes.Add("style", "display: none");
    }
    protected void btnOK_Click4(object sender, EventArgs e)
    {
        span5.Attributes.Add("style", "display: none");
        span6.Attributes.Add("style", "display: none");
        if (TextBox4.Text == "" || TextBox5.Text == "" || TextBox6.Text == "")
        {
            span5.Attributes.Add("style", "display: block");
            return;
        }
        string sql = "SELECT rule_nguyennhan.CF as CF FROM rule_nguyennhan, trieuchung WHERE rule_nguyennhan.IDTrieuChung = trieuchung.ID AND trieuchung.ID = " + IDTrieuChung;
        _db = new class_database();
        DataTable dt = _db.GetDataTable(sql);
        double checkSumCF = 0;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            checkSumCF += Math.Round(Convert.ToDouble(dt.Rows[i]["CF"]), 2);
        }
        checkSumCF += Math.Round(Convert.ToDouble(TextBox6.Text), 2);
        double difference = 1 * 0.01;
        if ((checkSumCF - 1.00) < difference)
        {
            string[] _param = { "IDTrieuChung", "Name", "CF" };
            string[] _value = { IDTrieuChung, TextBox4.Text.ToString(), TextBox6.Text.ToString() };
            _db.InsertRecord("rule_nguyennhan", _param, _value);
            string[] _param2 = { "IDNguyenNhan", "KhacPhuc" };
            sql = "SELECT ID FROM rule_nguyennhan ORDER BY ID DESC LIMIT 1";
            dt = _db.GetDataTable(sql);
            string[] _value2 = {dt.Rows[0]["ID"].ToString(), TextBox5.Text.ToString()};
            _db.InsertRecord("khacphuc", _param2, _value2);
        }
        else
        {
            span6.Attributes.Add("style", "display: block");
            return;
        }
        PopupThemNguyenNhan.Attributes.Add("style", "display: none");
    }
    protected void btnXoaNguyenNhan_Click(object sender, EventArgs e)
    {

    }
}